//
//  MinePassViewController.m
//  MJApp
//
//  Created by wangliang on 15/3/30.
//  Copyright (c) 2015年 wangliang. All rights reserved.
//

#import "MinePassViewController.h"
#import "MinePassCollectionViewCell.h"

@interface MinePassViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UIView *setingView;
@property (weak, nonatomic) IBOutlet UIView *setingBackView;
@property (weak, nonatomic) IBOutlet UICollectionView *numCollectionView;
@property (weak, nonatomic) IBOutlet UICollectionView *numMcollectView;
@property (weak, nonatomic) IBOutlet UICollectionView *numRcollectionView;
@property (weak, nonatomic) IBOutlet UIImageView *twoImageView;
@property (weak, nonatomic) IBOutlet UIImageView *threeImageView;
@property (weak, nonatomic) IBOutlet UIImageView *oneImageView;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;

@end

@implementation MinePassViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self UI];
}

- (void)UI {
    
    NSInteger allNum = [[[NSUserDefaults standardUserDefaults] objectForKey:@"secrect"] integerValue];
    
    NSString *str1 = [NSString stringWithFormat:@"mm_%ld.png",allNum / 100];
    NSString *str2 = [NSString stringWithFormat:@"mm_%ld.png",allNum % 100 / 10];
    NSString *str3 = [NSString stringWithFormat:@"mm_%ld.png",allNum % 100 % 10];
    self.oneImageView.image = LOAD_IMAGE(str1);
    self.twoImageView.image = LOAD_IMAGE(str2);
    self.threeImageView.image = LOAD_IMAGE(str3);

    
    self.title = @"密码锁";
    [(TabBarController *)self.tabBarController setTabbarHidden:YES];
    self.setingBackView.backgroundColor = Color_Back_Gray;
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(CGRectGetWidth(self.numCollectionView.frame), CGRectGetHeight(self.numCollectionView.frame));
    flowLayout.minimumLineSpacing = 0;
    self.numCollectionView.delegate = self;
    self.numCollectionView.dataSource = self;
    self.numCollectionView.collectionViewLayout = flowLayout;
    self.numCollectionView.pagingEnabled = YES;
    self.numCollectionView.backgroundColor = [UIColor clearColor];
    self.numCollectionView.showsVerticalScrollIndicator = NO;
    [self.numCollectionView registerNib:[UINib nibWithNibName:@"MinePassCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"MinePassCollectionViewCell"];
    
    self.numMcollectView.delegate = self;
    self.numMcollectView.dataSource = self;
    self.numMcollectView.collectionViewLayout = flowLayout;
    self.numMcollectView.pagingEnabled = YES;
    self.numMcollectView.backgroundColor = [UIColor clearColor];
    self.numMcollectView.showsVerticalScrollIndicator = NO;
    [self.numMcollectView registerNib:[UINib nibWithNibName:@"MinePassCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"MinePassCollectionViewCell"];
    
    self.numRcollectionView.delegate = self;
    self.numRcollectionView.dataSource = self;
    self.numRcollectionView.collectionViewLayout = flowLayout;
    self.numRcollectionView.pagingEnabled = YES;
    self.numRcollectionView.backgroundColor = [UIColor clearColor];
    self.numRcollectionView.showsVerticalScrollIndicator = NO;
    [self.numRcollectionView registerNib:[UINib nibWithNibName:@"MinePassCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"MinePassCollectionViewCell"];
    
    self.setingView.transform = CGAffineTransformMakeScale(0, 0);
    self.setingView.alpha = 0;
    
    [self.numCollectionView setContentOffset:CGPointMake(0, CGRectGetHeight(self.numCollectionView.frame) * (allNum / 100))];
    [self.numMcollectView setContentOffset:CGPointMake(0, CGRectGetHeight(self.numMcollectView.frame) * (allNum % 100 / 10))];
    [self.numRcollectionView setContentOffset:CGPointMake(0, CGRectGetHeight(self.numRcollectionView.frame) * (allNum % 100 % 10))];
}

- (IBAction)reset:(id)sender {
    
    [UIView animateWithDuration:0.2 animations:^{
        
        self.setingView.transform = CGAffineTransformMakeScale(1, 1);
        self.setingView.alpha = 1;
    }];
}

- (IBAction)close:(id)sender {
    
    [UIView animateWithDuration:0.2 animations:^{
        
        self.setingView.transform = CGAffineTransformMakeScale(0, 0);
        self.setingView.alpha = 0;
    }];
}

- (IBAction)ok:(id)sender {
    
    NSInteger oneNum = self.numCollectionView.contentOffset.y / CGRectGetHeight(self.numCollectionView.frame);
    NSInteger twoNum = self.numMcollectView.contentOffset.y / CGRectGetHeight(self.numMcollectView.frame);
    NSInteger threeNum = self.numRcollectionView.contentOffset.y / CGRectGetHeight(self.numRcollectionView.frame);
    
    NSString *str1 = [NSString stringWithFormat:@"mm_%ld.png",oneNum];
    NSString *str2 = [NSString stringWithFormat:@"mm_%ld.png",twoNum];
    NSString *str3 = [NSString stringWithFormat:@"mm_%ld.png",threeNum];
    self.oneImageView.image = LOAD_IMAGE(str1);
    self.twoImageView.image = LOAD_IMAGE(str2);
    self.threeImageView.image = LOAD_IMAGE(str3);
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy.MM.dd"];
    
    NSDate *date = [NSDate date];
    NSString *dateStr = [dateFormatter stringFromDate:date];
    self.timeLabel.text = [NSString stringWithFormat:@"设置时间:%@",dateStr];
    [self close:nil];
    
    NSInteger allNum = oneNum * 100 + twoNum * 10 + threeNum ;
    [[NSUserDefaults standardUserDefaults] setValue:@(allNum) forKey:@"secrect"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return 10;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    MinePassCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MinePassCollectionViewCell" forIndexPath:indexPath];
    NSString *str = [NSString stringWithFormat:@"mm_%ld.png",indexPath.row];
    cell.imageView.image = LOAD_IMAGE(str);
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
